package com.zyk.leetcode;

import java.util.ArrayDeque;
import java.util.Deque;

/**
 * @author zhangsan
 * @date 2021/4/5 19:14
 */
public class M30 {

    static class MinStack {
        // 存放内容
        Deque<Integer> stack1;
        // 存放最小元素
        Deque<Integer> stack2;

        // addLast, popFirst
        /** initialize your data structure here. */
        public MinStack() {
            stack1 = new ArrayDeque<>();
            stack2 = new ArrayDeque<>();
        }

        public void push(int x) {
            stack1.push(x);
            if(stack2.isEmpty()) {
                stack2.push(x);
            }else {
                stack2.push( (stack2.peekFirst() < x? stack2.peekFirst(): x) );
            }
        }

        public void pop() {
            stack1.pollFirst();
            stack2.pollFirst();
        }

        public int top() {
            return stack1.peekFirst();
        }

        public int min() {
            return stack2.peekFirst();
        }
    }

    // for test
    public static void main(String[] args) {
        MinStack minStack = new MinStack();
        minStack.push(-2);
        minStack.push(0);
        minStack.push(-3);
        //   --> 返回 -3.
        System.out.println(minStack.min());
        minStack.pop();
        // --> 返回 0.
        System.out.println(minStack.top());
        // --> 返回 -2.
        System.out.println(minStack.min());
    }

}
